/* 变量 */
var signin = "other/signin.html";


// 签到
var contactsCallback_signin = myApp.onPageInit('signin', function (page) {
    if (!hasAccount() || !hasOpenId()) {
        return;
    }
    //处理参数
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
    var cansignin = false; // 签到
    var isPost = true;
    var v_scenicLongitudeNow = "0";
    var v_scenicLatitudeNow = "0";

    if (ISS_Constants.User.longitude)
        v_scenicLongitudeNow = ISS_Constants.User.longitude;
    if (ISS_Constants.User.latitude)
        v_scenicLatitudeNow = ISS_Constants.User.latitude;

    //footpoint("31.562753","114.757897");   

    // 初始化数据
    $$("#signin_page .today").html(ISS.utils.dateToStr("yyyy年MM月dd日"));
    $$("#signin_page .day").html(getCalendar());

    // 提交签到
    $$("#signin_page #btn-signin").click(function () {
        if (cansignin) { // 可签到
            // 初始化数据
            $$.ajax({
                url: ISS_Constants.Url.const_weixin_signin_save,
                data: { "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "','Longitude':'" + v_scenicLongitudeNow + "','Latitude':'" + v_scenicLatitudeNow + "'}" },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {
                    if (data && data.success) {
                        LoadData();

                        footpoint(v_scenicLatitudeNow, v_scenicLongitudeNow);
                    }
                },
                error: function (xhr, status) {
                    console.log("ajax error!");
                }
            });
        }
    });

    // 初始化数据
    LoadData();

    // 初始化数据
    function LoadData() {
        if (isPost) {
            isPost = false;
            $$.ajax({
                url: ISS_Constants.Url.const_weixin_signin,
                data: { "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "'}" },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {
                    // 加载完毕
                    isPost = true;

                    if (data && data.success && data.datas) {
                        var sign = data.datas;
                        cansignin = sign.CanSignIn;
                        /* 签到 */
                        if (!sign.CanSignIn) { // 已签到
                            var jf = (sign.Logs[0].BaseIntegration + sign.Logs[0].RuleIntegration);
                            $$("#signin_page #signin_jf").html("<span>今日获得</span><br /><b>" + jf + "积分</b>");
                            var qd = 0;
                            if (sign.Logs && sign.Logs.length > 0) {
                                qd = sign.Logs[0].SignInCount;
                            }
                            $$("#signin_page #signin_qd").html("<span>本月连续签到</span><br /><b>" + qd + "天</b>");
                            //切换是否可以签到按钮
                            $$("#signin_page #btn-signin").hide();
                            $$("#signin_page #btn-signedin").show();
                        }
                        else {
                            var jf = 0;
                            $$("#signin_page #signin_jf").html("<span>今日获得</span><br /><b>" + jf + "积分</b>");
                            var qd = 0;
                            if (sign.Logs && sign.Logs.length > 0) {
                                qd = sign.Logs[0].SignInCount;
                            }
                            $$("#signin_page #signin_qd").html("<span>本月连续签到</span><br /><b>" + qd + "天</b>");
                            //切换是否可以签到按钮
                            $$("#signin_page #btn-signin").show();
                            $$("#signin_page #btn-signedin").hide();
                        }
                        //每日签到可送积分
                        var ej = sign.Info.Integration;
                        $$("#signin_page #sign-tip").html("<p>每日签到送<b>" + ej + "积分</b></p>");

                        /* 日历 */
                        if (sign.Logs && sign.Logs.length > 0) {
                            for (var i = 0; i < sign.Logs.length; i++) {
                                var date = ISS.utils.strToDate(sign.Logs[i].CreateTime);
                                var currDate = date.getDate();
                                $$.each($$("#signin_page .day li"), function (n, value) {
                                    if ($$(value).html() == currDate) {
                                        $$(value).removeClass("active");
                                        $$(value).addClass("active");
                                    }
                                });
                            }
                        }

                        /* 规则说明 */
                        var rule = "<h5>签到规则说明</h5>";
                        rule += "<p>1、每日签到可获得 " + sign.Info.Integration + " 积分。</p>";
                        if (sign.Rules && sign.Rules.length > 0) {
                            for (var i = 0; i < sign.Rules.length; i++) {
                                rule += "<p>" + (i + 2) + "、连续签到 " + sign.Rules[i].SignInCount + " 次，可获得 " + sign.Rules[i].Integration + " 积分。</p>";
                            }
                        }
                        $$("#signin_page .rule-box").html(rule);
                    }
                },
                error: function (xhr, status) {
                    console.log("ajax error!");
                }
            });
        }
    }; // endLoadData

    // 日历
    function getCalendar() {
        var currDate = new Date();
        var firstDay = new Date(currDate.getFullYear(), currDate.getMonth(), 1); // 当前月的第一天
        var endDay = new Date(currDate.getFullYear(), (currDate.getMonth() + 1), 0); // 当前月的最后一天
        var days = endDay.getDate(); // 当前月的天数
        var week = firstDay.getDay();  // 当前月的第一天日期

        var html = "";
        if (week > 0) {
            for (var i = 0; i < firstDay.getDay(); i++) {
                html += "<li class='col-7'></li>";
            }
        }
        for (var i = 0; i < days; i++) {
            html += "<li class='col-7'>" + (i + 1) + "</li>";
        }

        return html;
    }

    // 获取足迹
    function footpoint(lat, lng) {
        if (lat != "0" && lng != "0") {
            $$.ajax({
                url: ISS_Constants.Url.const_bdmap_geocoding_new,
                data: { "location": "" + lat + "," + lng + "" },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {
                    if (data.status == 0 && data.result) {
                        var address = data.result.formatted_address;
                        var district = data.result.addressComponent.district;
                        var city = data.result.addressComponent.city;
                        var province = data.result.addressComponent.province;
                        var country = data.result.addressComponent.country;
                        savefootpoint(country, province, city, district, address, lat, lng);
                    }
                },
                error: function (xhr, status) { }
            });
        }
    }; // end footpoint

    // 获取足迹
    function savefootpoint(Country, Province, City, District, Address, Longitude, Latitude) {
        if (Country != "" && Province != "" && City != "" && District != "") {
            $$.ajax({
                url: ISS_Constants.Url.const_weixin_footpoint_save,
                data: { "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "','Country':'" + Country + "','Province':'" + Province + "','City':'" + City + "','District':'" + District + "','Address':'" + Address + "','Longitude':'" + Longitude + "','Latitude':'" + Latitude + "'}" },
                crossDomain: true,
                dataType: "json",
                success: function (data, status) {

                },
                error: function (xhr, status) { }
            });
        }
    }; // end footpoint

});


function initData() {
    var href = window.location.href;
    if (href.indexOf(signin) > -1) {
        contactsCallback_signin.trigger();
    }
}

initData();